<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=517078

Tests for plugin stream error conditions.
-->
<head>
  <title>NPAPI Stream Error Tests</title>
  <script type="text/javascript" 
          src="/MochiKit/packed.js"></script>
  <script type="text/javascript" 
          src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" 
        href="/tests/SimpleTest/test.css" />
</head>
<body onload="runNextTest()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=517078">
  Mozilla Bug 517078</a> - Plugin Stream Error Tests
<p id="display"></p>
<div id="content" style="display: none">

</div>
<div id="test">
<script class="testbody" type="text/javascript">
////
// These tests verify that nothing "bad" happens when a plugin returns an
// error from one of the NPP_ stream functions.  "Bad" is defined here
// as the plugin being terminated, or NPP_ stream functions being
// called inappropriately by the browser after the plugin has returned
// a stream error.
//

function $(id) { return document.getElementById(id); }

SimpleTest.waitForExplicitFinish();


var tests = [
  {
    "src": "loremipsum.txt",
    "streammode": "normal",
    "functiontofail": "npp_newstream",
    "failurecode": "1",
    "frame": "testframe"
  }, 
  {
    "src": "loremipsum.txt",
    "streammode": "normal",
    "functiontofail": "npp_newstream",
    "failurecode": "3",
    "frame": "testframe"
  }, 
  {
    "src": "loremipsum.txt",
    "streammode": "normal",
    "functiontofail": "npp_newstream",
    "failurecode": "5",
    "frame": "testframe"
  }, 
  {
    "geturl": "loremipsum.txt",
    "streammode": "normal",
    "functiontofail": "npp_newstream",
    "failurecode": "1",
    "frame": "testframe"
  }, 
  {
    "src": "loremipsum.txt",
    "streammode": "normal",
    "functiontofail": "npp_write",
    "frame": "testframe"
  }, 
  {
    "src": "loremipsum.txt",
    "streammode": "asfile",
    "functiontofail": "npp_write",
    "frame": "testframe"
  }, 
  {
    "src": "loremipsum.txt",
    "streammode": "normal",
    "functiontofail": "npp_destroystream",
    "failurecode": "1",
    "frame": "testframe"
  }, 
];

function iframeonload(evt) {
  var contentLength = evt.target.contentDocument.body.innerHTML.length;
  var plugin = gTestWindow.document.getElementById("embedtest");
  var functionToFail = plugin.getAttribute("functiontofail");
  if (contentLength > 0) {
    is(evt.target.contentDocument.body.innerHTML, "pass",
      "test frame has unexpected content");
    setTimeout(function() {
      // This verifies that the plugin hasn't been unloaded, and that
      // no calls to NPP_ functions have been made unexpectedly.
      is(plugin.getError(), "pass", "plugin reported an error");
      gTestWindow.close();
      setTimeout(runNextTest, 10);
    }, functionToFail == "npp_newstream" ? 500 : 10);
  }
}

var index = 0;
var gTestWindow;
function runNextTest() {
  if (index == tests.length) {
    SimpleTest.finish();
    return;
  }

  gTestWindow = window.open("plugin_window.html", 
                            "", 
                            "width=620,height=320");
}

function continueTest() {
  var test = tests[index];
  
  var p = gTestWindow.document.createElement("p");
  p.innerHTML = "Plugin Stream Test " + index;
  gTestWindow.document.getElementById("test").appendChild(p);
  
  if (test.frame) {
    var iframe = gTestWindow.document.createElement("iframe");
    iframe.name = test.frame;
    iframe.onload = iframeonload;
    gTestWindow.document.getElementById("test").appendChild(iframe);
  }
  
  var embed = gTestWindow.document.createElement("embed");
  embed.setAttribute("id", "embedtest");
  embed.setAttribute("style", "width: 400px; height: 100px;");
  embed.setAttribute("type", "application/x-test");
  for (var name in test) {
    embed.setAttribute(name, test[name]);
  }
  gTestWindow.document.getElementById("test").appendChild(embed);
  
  gTestWindow.document.getElementById("test")
                      .appendChild(document.createElement("br"));
  
  index++; 
}

</script>
</div>
</body>
</html>

